import {ActionTree, Module} from 'vuex'

import {AssetStockState, RootState} from '../types'
import {url} from '@/store/utils'
import {AxiosPromise} from 'axios'

const namespaced: boolean = true

const actions: ActionTree<AssetStockState, RootState> = {
  fetchAssetStock(context, id) {
    return axios.get(url(`/api/asset-stock/get/${id}`))
  },
  delete(state, id){
    return axios.post(url(`/api/asset-stock/delete/${id}`))
  },
  fetchAssetStockList(state, payload) {
    return axios.post(url('/api/asset-stock/list'), payload)
  },
  fetchAssetStockChangeHistory(context: any, payload: any) {
    let assetId = payload[0]
    let options = payload[1]
    return axios.post(url(`/api/asset-stock-change-history/list/${assetId}`), options)
  },
  addChangeHistory(context: any, payload: any) {
    let assetId = payload[0]
    let changeHisory = payload[1]
    return axios.post(url(`/api/asset-stock-change-history/add/${assetId}`), changeHisory)
  },
  save(state, asset) {
    return axios.post(url('/api/asset-stock'), asset)
  },
  downloadAssetImportTemplate() {
    return axios({
      url: url('/api/asset-stock/excel/export_template'),
      method: 'GET',
      responseType: 'blob' // important
    })
  },
  downloadExcel(){
    return axios({
      url: url('/api/asset-stock/excel/export'),
      method: 'GET',
      responseType: 'blob' // important
    })
  },
  import(state, importFile) {
    let formData = new FormData()
    formData.append('upload_file', importFile)
    return axios.post(url('/api/asset-stock/excel/import'), formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}
export const AssetStock: Module<AssetStockState, RootState> = {
  namespaced,
  actions
}

export default AssetStock
